﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class AreaService : BaseService, IAreaService
    {
        private readonly IWarehouseRepository _warehouseRepository;
        private readonly IAreaRepository _areaRepository; 
        private readonly IDictItemRepository _ictItemRepository;
        private readonly IUnitOfWork _unitOfWork;
        public AreaService(IMapper mapper, IAreaRepository _areaRepository, IWarehouseRepository warehouseRepository, IDictItemRepository ictItemRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            this._areaRepository = _areaRepository;
            _warehouseRepository = warehouseRepository;
            _ictItemRepository = ictItemRepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddArea(Area obj, out string message)
        {
            message=string.Empty;
            var aobj =_areaRepository.QueryIsEntityWhere(x => x.Code == obj.Code);
            if (aobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            if(obj.WhId==null)
            {
                message = "请选择库房！";
                return false;
            }
            //库房
            var wobj = _warehouseRepository.QueryEntityById((long)obj.WhId);
            if (wobj != null && wobj.Status == 1)
            {
                obj.WhCode = wobj.Code;
                obj.WhName = wobj.Name;
            }
            else
            {
                message = "库房不存在或已被禁用";
                return false;
            }
            //库区类型
            var dictemobj = _ictItemRepository.QueryEntityById(obj.TypeId);
            if (dictemobj != null)
            {
                obj.TypeName = dictemobj.ItemName;
            }
            _areaRepository.AddEntity(obj);
            return _unitOfWork.SaveChangesEntity();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelArea(long id)
        {
            var aobj =_areaRepository.QueryEntityById(id);
            if (aobj == null)
            {
                return false;
            }
          
            _areaRepository.DelEntity(aobj);
            return _unitOfWork.SaveChangesEntity();
        }

        /// <summary>
        /// 根据主键获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Area? GetAreaById(long id)
        {
            return _areaRepository.QueryEntityById(id);
        }

        /// <summary>
        /// 库区列表显示
        /// </summary>
        /// <param name="kname"></param>
        /// <param name="wid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Area> GetAreanList(string? kname, long wid, int pageIndex, int pageSize, out int totalCount)
        {
            var alist = _areaRepository.GetEntityAlls();

            //名称
            if (!string.IsNullOrEmpty(kname))
            {
                alist = alist.Where(x => x.Name.Contains(kname));
            }
            //库房
            if (wid != 0)
            {
                alist = alist.Where(x => x.WhId == wid);
            }
            var list = _areaRepository.GetEntityAlls(alist, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        /// <summary>
        /// 根据库房获取库区
        /// </summary>
        /// <param name="wid"></param>
        /// <returns></returns>
        public List<Area> GetWhIdAreaList(long wid)
        {
            var alist = _areaRepository.GetEntityWhereAll(x => x.WhId == wid);

            return alist.ToList();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="u"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool UpdateArea(Area u, out string message)
        {
            message = string.Empty;
            var aobj = _areaRepository.QueryIsEntityWhere(x => x.Id != u.Id && x.Code == u.Code);
            if (aobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            if (u.WhId == null)
            {
                message = "请选择库房！";
                return false;
            }
            //库房
            var wobj = _warehouseRepository.QueryEntityById((long)u.WhId);
            if (wobj != null && wobj.Status == 1)
            {
                u.WhCode = wobj.Code;
                u.WhName = wobj.Name;
            }
            else
            {
                message = "库房不存在或已被禁用！";
                return false;
            }
            //库区类型
            var dictemobj = _ictItemRepository.QueryEntityById(u.TypeId);
            if (dictemobj != null)
            {
                u.TypeName = dictemobj.ItemName;
            }
            _areaRepository.UpdEntity(u);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
